<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\BannerRequest;
use App\Models\Banner;
use App\Repository\BannerRepository;
use App\Service\ImgUploadService;
use Illuminate\Http\Request;

class BannersController extends Controller
{
    private $bannerRepository;
    public function __construct(BannerRepository $bannerRepository)
    {

        $this->bannerRepository = $bannerRepository;
        
    }
    //
    public function index(Request $request)
    {
        if($request->ajax()){
            //获取信息
            $list = $this->bannerRepository->getList($request->input());
            return $this->tableAjax($list);

        }else{
            return view('admin.banners.index');
        }
        
    }
    public function create(Request $request)
    {
        return view('admin.banners.create_and_edit');
    }

    public function store(ImgUploadService $imgUploadService,BannerRequest $request)
    {
        
        $img_path = $imgUploadService->img_upload($request);
        if(!$img_path){
            $this->error('图片保存失败,请重新上传');
        }
        //保存数据
       $banner = new Banner();
       $banner->title = $request->title;
       $banner->content = $request->content;
       $banner->img_path = $img_path;
       if($banner->save()){
           return $this->success('添加成功');
       }else{
           return $this->error('添加失败');
       }

    }
}
